<?php
//1、简单工厂模式 start********************************************
//https://www.cnblogs.com/ryanzheng/p/12783055.html
class mysql_conn{
    private $dbh = null;
    private $host = "";
    private $port = "";
    private $user = "";
    private $password = "";
    private $dbname = "";
    public function __construct(){
        $this->dbh = new PDO('mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }
    public function get_table_data($table_name){
        foreach($this->dbh->query("SELECT * FROM `$table_name`") as $row){
            echo "<pre>";
            print_t($row);
        }
    }
}
class psql_conn {
    private $dbh = null;
    private $host = 'localhost';
    private $port = '5432';
    private $user = 'postgres';
    private $password = '****';
    private $dbname = 'test';

    public function __construct() {
        $this->dbh = new PDO('pgsql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name) {
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql) {
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}
//通过下方的代码可以看出，简单工厂的缺点就是后面如果
//想增加新的连接，需要频繁地去修改静态方法
class FactorySimple {
    public static function pdo_conn($db = 'mysql'){
        if( $db === 'mysql'){
            return new mysql_conn();
        }else if( $db === 'psql'){
            return new psql_conn();
        }
    }
}
//mysql
$mysql_dbh = FactorySimple::pdo_conn('mysql');
$mysql_dbh->get_table_data('user');
$mysql_dbh->get_table_data('order');

//postgresql
$psql_dbh = FactorySimple::pdo_conn('psql');
$psql_dbh->custom_query("SELECT * FROM student;");

//1、简单工厂模式 end**********************************************



//2、工厂方法 start  **********************************************
interface database_option{
    public function custom_query($sql);
    public function get_table_data($table_name);
}
class mysql_conn implements database_option {
    private $dbh = null;
    private $host = 'localhost';
    private $port = '3306';
    private $user = 'root';
    private $password = '****';
    private $dbname = 'test';

    public function __construct() {
        $this->dbh = new PDO('mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name) {
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql) {
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}
class psql_conn implements database_option {
    private $dbh = null;
    private $host = 'localhost';
    private $port = '5432';
    private $user = 'postgres';
    private $password = '****';
    private $dbname = 'test';

    public function __construct() {
        $this->dbh = new PDO('pgsql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password);
    }

    public function get_table_data($table_name) {
        foreach($this->dbh->query("SELECT * from `$table_name`") as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }

    public function custom_query($sql) {
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        foreach ($stmt as $row) {
            echo "<pre>";
            print_r($row);
            echo "</pre>";
        }
    }
}
abstract class Factory{
    abstract static function pdo_conn();
}
class mysql_Factory extends Factory{
    public static function pdo_conn() {
        return new mysql_conn();
    }
}
class psql_Factory extends Factory {
    public static function pdo_conn(){
        return new psql_conn();
    }
}
//mysql
$mysql_dbh = mysql_Factory::pdo_conn();
$mysql_dbh->get_table_data('order');

//psql
$psql_dbh = psql_Factory::pdo_conn();
$psql_dbh->custom_query("SELECT * FROM student ");
//2、工厂方法 end    **********************************************